//
//  TFStatusViewModel.swift
//  weiboSina
//
//  Created by admin on 16/1/2.
//  Copyright © 2016年 admin. All rights reserved.
//  cell对应的视图模型

import UIKit

class TFStatusViewModel: NSObject {
    
    //当前视图模型对应的模型数据
    var status: TFStatus?{
    
        didSet{
        //计算会员要显示的图片
            if let mbrank = status?.user?.mbrank where mbrank>0 && mbrank<7 {
            memberImage = UIImage(named: "common_icon_membership_level\(mbrank)")
            
            }else{
                /**
                设置会员图标
                */
                memberImage = nil
            
            }
            /**
            设置认证图标
            */
            verifiedImage = calcVerFiedImage()
            
            repostsCount = calcStatudesCount(status?.reposts_count ?? 0, defaultTitle: "转发")
            commentsCount = calcStatudesCount(status?.comments_count ?? 0, defaultTitle: "评论")
            attitudesCount = calcStatudesCount(status?.attitudes_count ?? 0, defaultTitle: "赞")
           
            //判断是否有转发微博
            if let retWeetText = status?.retweeted_status?.text{
                if let name = status?.retweeted_status?.user?.screen_name{
                
                retweetStatusText = "@\(name):\(retWeetText)"
                }
            
            }
            
//            // 判断原创微博是否有图片
//            if let urls = status?.pic_urls where urls.count > 0 {
//                picUrls = urls
//            }else if let urls = status?.retweeted_status?.pic_urls where urls.count > 0 {
//                // 判断转发微博是否有图片
//                picUrls = urls
//            }

            
           dealSourceText()
            
            //处理时间
            if let date = NSDate.sinaDate((status?.created_at!)!){
            
            let dt = NSDateFormatter()
                
                dt.dateFormat = "yyyy-MM-dd HH:mm:ss"
                //指定本地化信息
                dt.locale = NSLocale(localeIdentifier: "en_US")
                creatText = dt.stringFromDate(date)
            }
        }
    
    }
    /**
     会员显示的图片
     */

    var memberImage: UIImage?
    /**
    认证的图标
    */
    var verifiedImage: UIImage?
    
    
    /**
     转发评论赞的数量的字符串
     */
    var repostsCount: String?
    var commentsCount:String?
    var attitudesCount: String?
    
    /**
     转发微博的设置
     */
    var retweetStatusText: String? //@昵称：转发微博的内容
    
    /**
    当前微博的配图数组
    */
    var picUrls:[TFStatusPictureInfo]?
    
    /**
     格式化好的来源字符串
     */
    var sourceText: String?
    //创建时间
    var creatText: String?
    //处理来源字符串
    private func dealSourceText(){
    
        guard let text = status?.source else{
        
        return
        }
        if let preRange = text.rangeOfString("\">"){
            if let sufRange = text.rangeOfString("</"){
            
            sourceText = "来自: \(text.substringWithRange(preRange.endIndex..<sufRange.startIndex))"
            }
        
        }
    
    }
    
    ///计算转发的评论的字符串
    func calcStatudesCount(count:Int,defaultTitle: String) -> String{
    
        if count == 0 {
        return defaultTitle
        }
        if count < 10000 {
        return "\(count)"
        }else{
        let result = CGFloat(count/1000) / 10
            let resultStr = "\(result)万"
            if resultStr.containsString(".0"){
            
            return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            return resultStr
         }
    
    }
    
    
    ///计算认证的图标
    private func calcVerFiedImage() -> UIImage? {
    
        if let verifiedType = status?.user?.verified_type{
        
            switch verifiedType {
            
            case -1:
                return nil
            case 0:
                return UIImage(named: "avatar_vip")
            case 2,3,5:
                return UIImage(named: "avatar_enterprise_vip")
            case 220:
                return UIImage(named: "avatar_grassroot")
            default:
                return nil
              }
        }
        
        return nil
    }
  
}
